package vip.meet.service;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.meet.dao.bean.user.UserBean;
import vip.meet.dao.mapper.user.UserBeanMapper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class TranService {

    @Resource
    UserBeanMapper userBeanMapper;

    private static final ExecutorService executorService = Executors.newSingleThreadExecutor();

    @Transactional
    public void addUser(UserBean user) throws InterruptedException {
        userBeanMapper.insertSelective(user);
        user.setNickname("ok不ok");
        userBeanMapper.insertSelective(user);
//        System.out.println(1/0);
        executorService.execute(() -> {
            for (int i = 0; i < 5; i++) {
                if (i == 4) {
                    System.out.println(1 / 0);
                }
                user.setNickname("pool-k-" + i);
                userBeanMapper.insertSelective(user);
                log.info("done:{}", i);

                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    log.error("异常中断:{}", e.getMessage(), e);
                }
            }
        });
        TimeUnit.SECONDS.sleep(10);
        System.out.println(1 / 0);
        log.info("addUser完成");
    }

}
